# Synchronizes two directories - copies files with higher modification date
# from source_dir to dest_dir.
# Usage: synchronize_directories.rb source_dir dest_dir.
# Copyright (c) Nikolai Holub.

require 'find'
require 'fileutils'
require 'ftools'

source_dir = ARGV[0]
dest_dir = ARGV[1]

Find.find(source_dir) do |source_file|
  if !File.directory?(source_file)
    dest_file = source_file.sub(source_dir, dest_dir)

    if (File.exists?(dest_file) && (File.mtime(dest_file) < File.mtime(source_file)))
      puts "copying #{source_file} to #{dest_file}"
      FileUtils.copy_file(source_file, dest_file, true)
    end
  end
end
